<?php
class SanPham extends CI_Controller
{
	function xulythem()
	{
		$tensp = $this->input->post('tensp');
		$soluong = $this->input->post('soluong');
		$gia = $this->input->post('gia');
		$mota = $this->input->post('mota');
		
		// Thuc hien upload hinh anh
		$config['upload_path'] = './asset/upload/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1024';
		$config['encrypt_name']	= true;
		$this->load->library('upload', $config);
		$hinh = '';
		if($this->upload->do_upload('hinh'))
		{
			$df = array('upload_data' => $this->upload->data());
			$hinh = $df['upload_data']['file_name'];
		}
		
		$hienthi = $this->input->post('hienthi')=='true' ? 'true' : 'false';
		$madm = $this->input->post('madm');
		$this->msanpham->themdolan($tensp, $gia, $soluong, $hinh, $mota, $hienthi, $madm);
		redirect('admin/sanpham/danhsachsp');		
	}
	
	function danhsach($madm)
	{
		$data = array(
			'page'=>'danhsachsanpham',
			'dsh'=>$this->msanpham->danhsachsanpham($madm)
		);	
		$this->load->view('admin/templateadmin', $data);
	}
	
	function xoadolan($masp)
	{
		$this->msanpham->xoadolan($masp);
		redirect('admin/sanpham/danhsachsp');	
	}
	
	function capnhatdolan($masp)
	{
		$data = array('tth'=>$this->msanpham->thongtindolan($masp),'page'=>'capnhatsanpham');
		$this->load->view('admin/templateadmin',$data);
	}
	
	function xulycapnhat()
	{
		$masp = $this->input->post('masp');
		$tensp = $this->input->post('tensp');
		$gia = $this->input->post('gia');
		$soluong = $this->input->post('soluong');
		$mota = $this->input->post('mota');
		$hienthi = $this->input->post('hienthi')=='true' ? 'true' : 'false';
		
		//cap nhat hinh anh
		$config['upload_path'] = './asset/upload/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']	= '1024';
		$config['encrypt_name']	= true;
		$this->load->library('upload', $config);
		
		if($this->upload->do_upload('hinh'))
		{
			$df = array('upload_data' => $this->upload->data());
			$hinh = $df['upload_data']['file_name'];
		}
		if ($hinh == '')
			$hinh = $this->input->post('oldhinh');
			
		$this->msanpham->capnhatdolan($masp, $tensp, $gia, $soluong, $hinh, $mota, $hienthi);
		redirect('admin/sanpham/danhsachsp');
	}
	
	function thongtin($masp)
	{
		$data = array(
			'page'=>'thongtinsanpham',
			'tth'=>$this->msanpham->thongtindolan($masp)
		);
		$this->load->view('admin/templateadmin', $data);
	}
	
	function them()
	{
		$data = array(
			'page'=>'themsanpham',
			'dsdm'=>$this->mdanhmucsanpham->danhsachdanhmucAdmin()
		);
		$this->load->view('admin/templateadmin', $data);
	}
	
	function danhsachsp()
	{
		$data = array(
			'page'=>'danhsachsanpham',
			'dsh'=>$this->msanpham->danhsachAdmin()
		);	
		$this->load->view('admin/templateadmin', $data);
	}
}
?>